%% Created Time:  Tue Apr 17 10:18:37 2012

-module(ts).
-export([start/0, insert/2, get_content/2, size/1, dump/1, name/1]).
-author("Charon <kevinlpcd@gmail.com>").
-import(id_allocate,[get_id/0]).
-vsn("ts 0.1").
-ifdef(debug).
-define(TRACE(X), io:format("TRACE ~p:~p ~p. [@~p]~n", [?MODULE, ?LINE, X, ?FILE])).
-else.
-define(TRACE(X), void).
-endif.

start() ->
	Name_S = get_id(),
	Name_A = list_to_atom(string:to_lower(Name_S)),
	ID = ets:new(Name_A, [set, protected]),
	{ok,ID,Name_A}.

insert(T_ID, Value) ->
	case ets:insert(T_ID, Value) of
		true ->
			{ok,T_ID};
		_ ->
			{error,T_ID}
	end.

get_content(T_name, Key) ->
	Content = ets:lookup(T_name, Key),
	Content.

size(T_ID) ->
	Size = ets:info(T_ID, size),
	Size.

name(T_ID) ->
	Name = ets:info(T_ID, name),
	Name.

dump(T_ID) ->
	c:cd("cache"),
	{ok, FILE} = dets:open_file(name(T_ID), [{type, set}]),
	ets:to_dets(T_ID, FILE),
	dets:close(name(T_ID)),
	c:cd(" .."),
	true.

load(T_ID, Name) ->
	c:cd("cache"),
	{ok, FILE} = dets:open_file(Name),
	ets:from_dets(T_ID, FILE),
	dets:close(name(T_ID)),
	c:cd(".."),
	true.

stop(T_ID) ->
	ets:delete(T_ID),
	ok.
